/*!***************************************************
 * @file: client_management.h
 * @brief: 用户管理头文件
 * @author: ITHAO 
 * @date: 2018.11.21
 * @note: 
 ****************************************************/
#ifndef __CLIENT_MANAGEMENT_H__
#define __CLIENT_MANAGEMENT_H__

#include <pthread.h>
#include <stdbool.h>

#include "list.h"

typedef unsigned char   uint8;
typedef unsigned short  uint16;
typedef unsigned int    uint32;

enum ret {
    ACCOUNT_EXIXT = 2,
    ACCOUNT_NOT_EXIXT,
    PASSWORD_INCORRECT,
    OVER_ONLINE_USER_LIMIT,
};

enum man_err{
    MAN_PARAM_NULL_ERR = 1,
    MAN_OPEN_FILE_FAIL_ERR,
    MAN_INIT_RWLOCK_ERR,
    MAN_DESTORY_RWLOCK_ERR,
    MAN_INIT_MUTEX_ERR,
    MAN_DESTORY_MUTEX_ERR,
    MAN_ENCIPHER_FAIL_ERR,
    MAN_WRITE_ERR,
};


#define LOGIN_INFO_FILE    "./log/account.txt"
#define LOG_DIR            "./log"

#define NAME_LENGTH        (11)
#define ACCOUNT_LENGTH     (11)
#define PASSWORD_LENGTH    (11)


#define MAX_ONLIEN_NUM      5000
//在线用户链表头,这里需要互斥
struct online_user {
    uint16 online_num;
    struct list_head online_list_head;
    pthread_mutex_t mutex;
};

//在线用户链表项
struct single_online_user {
    char name[NAME_LENGTH];
    char account[ACCOUNT_LENGTH];
    int clientfd;
    struct list_head list;
};

//用户账号信息
struct login_info {
    char name[NAME_LENGTH]; 
    char account[ACCOUNT_LENGTH];
    char password[PASSWORD_LENGTH];
};


extern int online_user_init(struct online_user *p_online_user);
extern int client_management_destroy(struct online_user *p_online_user);
extern int user_register(const struct login_info *p_login_info);
extern int user_login(struct login_info *p_login_info);
extern bool is_account_exist(const char *account, char *get_name);
extern int add_online_user(struct login_info *p_login_info, int clientfd,
                    struct online_user *p_online_user);
extern int remove_online_user(int clientfd, char *account,
                       struct online_user *p_online_user);
extern bool is_online_user(const char account[], 
                      struct online_user *p_online_user, int *clientfd);
extern void display_online_user(struct online_user *p_online_user);


#endif